<?php
/*
*收支平衡接口类
*/
class profitAppClass extends appBase{
	/*分类统计*/
	public function profitTotal(){
		//参数准备
		$incomeArray = $costArray = array();
		$costAll = $incomeAll = 0;
		//判断
		if(!$this->power("profit","seeAll")){
			$json['warn'] = "您没有查看所有收支平衡的权限";
		}elseif(empty($this->post['startDay'])){
			$json['warn'] = "请填写结算日统计开始日期";
		}elseif(empty($this->post['endDay'])){
			$json['warn'] = "请填写结算日统计结束日期";
		}elseif(strtotime($this->post['startDay']) > strtotime($this->post['endDay'])){
			$json['warn'] = "开始日期不能大于结束日期";
		}elseif(strtotime($this->post['endDay']) > time()){
			$json['warn'] = "结束日期不得大于当前日期";
		}else{
			//数据查询
			$cost = $income = array();
			$sql = find(array("table" => "profit","where" => "payDate >= '".$this->post['startDay']."' and payDate <= '".$this->post['endDay']."'"));
			foreach($sql['array'] as $array){
				if($array['direction'] == "支出"){
					$cost[$array['type']] += $array['money'];
					$costAll += $array['money'];
				}elseif($array['direction'] == "收入"){
					$income[$array['type']] += $array['money'];
					$incomeAll += $array['money'];
				}
			}
			//收入
			$incomeArray = array();
			arsort($income,SORT_NUMERIC);
			foreach($income as $type => $money){
				$incomeArray[] = array(
					"type" => $type,//收入类型
					"money" => $money,//收入金额
				);
			}
			//支出
			$costArray = array();
			arsort($cost,SORT_NUMERIC);
			foreach($cost as $type => $money){
				$costArray[] = array(
					"type" => $type,//收入类型
					"money" => $money,//收入金额
				);
			}
			//提示
			$json['warn'] = "success";
		}
		//返回
		$json['total'] = array(
			"income" => array(
				"money" => $incomeAll,//合计收入
				"array" => $incomeArray,//收入明细
			),
			"cost" => array(
				"money" => $costAll,//合计支出
				"array" => $costArray,//支出明细
			),
			"balance" => round($incomeAll - $costAll,2),//结余
		);
		return $json;
	}
	/*收支曲线~按年统计*/
	public function profitYear(){
		//参数准备
		$abscissa = $income = $pay = $wages = $profit = array();
		//判断
		if(!$this->power("profit","seeAll")){
			$json['warn'] = "您没有查看所有收支平衡的权限";
		}elseif(empty($this->post['startYear'])){
			$json['warn'] = "请填写开始年份";
		}elseif(empty($this->post['endYear'])){
			$json['warn'] = "请填写结束年份";
		}elseif(strtotime($this->post['startYear']) > strtotime($this->post['endYear'])){
			$json['warn'] = "开始年份不能大于结束年份";
		}elseif($this->post['endYear'] > date("Y")){
			$json['warn'] = "结束年份不能大于当前年份";
		}else{
			//数据查询
			for($n=$this->post['startYear'];$n<=$this->post['endYear'];$n++){
				$abscissa[] = $n;
				$start = $n."-01-01";
				$end = $n."-12-31";
				//收入
				$in = find(array("table" => "profit","sum" => "money","where" => "direction = '收入' and payDate >= '$start' and payDate <= '$end'"));
				$income[] = array("money" => $in);
				//支出
				$out = find(array("table" => "profit","sum" => "money","where" => "direction = '支出' and payDate >= '$start' and payDate <= '$end'"));
				$pay[] = array("money" => $out);
				//工资
				$wages[] = array(
					"money" => find(array("table" => "profit","sum" => "money","where" => "direction = '支出' and type = '工资拨付' and payDate >= '$start' and payDate <= '$end'")),
				);
				//结余
				$profit[] = array("money" => round($in - $out,2));
			}
			//返回
			$json = array(
				"warn" => "success",//提示
				"title" => "收支曲线~按年统计",//标题
				"abscissa" => $abscissa,//纵坐标
				"array" => array(
					array(
						"name" => "收入",//数据名称
						"data" => $income,//数据
					),
					array(
						"name" => "支出",
						"data" => $pay,
					),
					array(
						"name" => "工资",
						"data" => $wages,
					),
					array(
						"name" => "结余",
						"data" => $profit,
					),
				),
			);
		}
		//返回
		return $json;
	}
	/*收支曲线~按月统计*/
	public function profitMoon(){
		//参数准备
		$abscissa = $income = $pay = $wages = $profit = array();
		//判断
		if(!$this->power("profit","seeAll")){
			$json['warn'] = "您没有查看所有收支平衡的权限";
		}elseif(empty($this->post['startMoon'])){
			$json['warn'] = "请填写开始月份";
		}elseif(empty($this->post['endMoon'])){
			$json['warn'] = "请填写结束月份";
		}elseif(strtotime($this->post['startMoon']) > strtotime($this->post['endMoon'])){
			$json['warn'] = "开始月份不能大于结束月份";
		}elseif(strtotime($this->post['endMoon']."-01 00:00:00") > time()){
			$json['warn'] = "结束月份不得大于当前月份";
		}else{
			//数据查询
			$moonArray = $this->moon(array("start" => $this->post['startMoon'],"end" => $this->post['endMoon']));
			foreach($moonArray as $moon){
				$abscissa[] = date("Y-m",strtotime($moon['start']));
				//收入
				$in = find(array("table" => "profit","sum" => "money","where" => "direction = '收入' and payDate >= '$moon[start]' and payDate <= '$moon[end]'"));
				$income[] = array("money" => $in);
				//支出
				$out = find(array("table" => "profit","sum" => "money","where" => "direction = '支出' and payDate >= '$moon[start]' and payDate <= '$moon[end]'"));
				$pay[] = array("money" => $out);
				//工资
				$wages[] = array(
					"money" => find(array("table" => "profit","sum" => "money","where" => "direction = '支出' and type = '工资拨付' and payDate >= '$moon[start]' and payDate <= '$moon[end]'")),
				);
				//结余
				$profit[] = array("money" => round($in - $out,2));
			}
			//返回
			$json = array(
				"warn" => "success",
				"title" => "收支曲线~按月统计",
				"abscissa" => $abscissa,
				"array" => array(
					array(
						"name" => "收入",
						"data" => $income,
					),
					array(
						"name" => "支出",
						"data" => $pay,
					),
					array(
						"name" => "工资",
						"data" => $wages,
					),
					array(
						"name" => "结余",
						"data" => $profit,
					),
				),
			);
		}
		//返回
		return $json;
	}
	/*员工数曲线*/
	public function staffNum(){
		//参数准备
		$abscissa = $staffMoon = $staffNum = array();
		//判断
		if(!$this->power("profit","seeAll")){
			$json['warn'] = "您没有查看所有收支平衡的权限";
		}elseif(empty($this->post['startMoon'])){
			$json['warn'] = "请填写开始月份";
		}elseif(empty($this->post['endMoon'])){
			$json['warn'] = "请填写结束月份";
		}elseif(strtotime($this->post['startMoon']) > strtotime($this->post['endMoon'])){
			$json['warn'] = "开始月份不能大于结束月份";
		}elseif(strtotime($this->post['endMoon']."-01 00:00:00") > time()){
			$json['warn'] = "结束月份不得大于当前月份";
		}elseif(strtotime($this->post['startMoon']) < strtotime("2017-10")){
			$json['warn'] = "2017年10月前无此数据";
		}elseif($this->post['endMoon'] == date("Y-m") and date("d") <= 10){
			$json['warn'] = "本月数据要十号发工资后才能统计";
		}else{
			//数据查询
			$moonArray = $this->moon(array("start" => $this->post['startMoon'],"end" => $this->post['endMoon']));
			foreach($moonArray as $moon){
				$abscissa[] = date("Y-m",strtotime($moon['start']));
				$sql = find(array("table" => "profit","field" => "distinct stid","where" => "type = '工资拨付' and payDate > '$moon[start]' and payDate < '$moon[end]'"));
				$staffNum[] = array("num" => $sql['num']);
			}
			//返回
			$json = array(
				"warn" => "success",
				"title" => "员工数曲线",
				"abscissa" => $abscissa,
				"array" => array(
					array(
						"name" => "员工数",
						"data" => $staffNum,
					),
				),
			);
		}
		//返回
		return $json;
	}
	/*查询条件*/
	public function search(){
		//收入
		$profitOutMoney = array();
		$profitOutMoneyArray = explode("，",para("profitOutMoney")."，订单");
		foreach($profitOutMoneyArray as $value){
			$profitOutMoney[] = array(
				"name" => $value,
			);
		}
		//支出
		$costType = array();
		$costTypeArray = explode("，",para("costType")."，".para("orderCost"));
		foreach($costTypeArray as $value){
			$costType[] = array(
				"name" => $value,
			);
		}
		//查询全部
		if($this->power("profit","seeAll")){
			$stid = "是";
		}else{
			$stid = "否";
		}
		//查询条件
		$json['search'] = array(
			"target" => array("回款记录","营业外收入","订单已付费用","费用报销","结算账户"),
			"direction" => array(
				array(
					"name" => "收入",
					"array" => $profitOutMoney,
				),
				array(
					"name" => "支出",
					"array" => $costType,
				),
			),
			"stid" => $stid,
		);
		//返回
		$json['warn'] = "success";
		return $json;
	}
	/*列表页*/
	public function home(){
		if(!$this->power("profit","see") and !$this->power("profit","seeAll")){
			$json['warn'] = "您没有收支平衡查看权限";
		}else{
			//like查询数组
			$like = array(
				"text" => $this->post['text'],//备注
			);
			//全等查询数组
			$equal = array(
				"stid" => $this->post['stid'],//员工ID
				"target" => $this->post['target'],//来源
				"direction" => $this->post['direction'],//变动方向
				"type" => $this->post['type'],//类型
			);
			//大于等于查询数组
			$big = array(
				"payDate" => $this->post['startDay'],//结算日-开始日期
			);
			//小于等于查询数组
			$small = array(
				"payDate" => $this->post['endDay'],//结算日-结束日期
			);
			//如果没有查看所有员工费用报销的权限
			if(!$this->power("profit","seeAll")){
				$and = " and stid = '".$this->control['stid']."' ";
			}
			//获取查询结果
			$where = search(array("like" => $like,"=" => $equal,">=" => $big,"<=" => $small)).$and;
			$page = $this->page(array("table" => "profit","where" => $where,"order" => "payDate desc,time desc"));
			//数据分析
			$json['profit'] = array();
			foreach($page['array'] as $array){
				$staff = query("staff","stid = '$array[stid]'");
				if(empty($array['ico'])){
					$ico = "否";
				}else{
					$ico = "是";
				}
				$staff = query("staff","stid = '$array[stid]'");
				$json['profit'][] = array(
					"id" => $array['id'],//主键
					"staffName" => $staff['name'],//负责人
					"target" => $array['target'],//来源
					"direction" => $array['direction'],//方向
					"type" => $array['type'],//类型
					"money" => $array['money'],//金额
					"ico" => $ico,//付款凭证
					"text" => $array['text'],//备注
					"payDate" => $array['payDate'],//发生日期
				);
			}
			//分页信息
			$json['page'] = $page['para'];
			//提示
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
	/*详情页*/
	public function detail(){
		$profit = query("profit","id = '".$this->post['id']."'");
		if(!$this->power("profit","seeAll") and $profit['stid'] != $this->control['stid']){
			$json['warn'] = "这不是您的收支信息，您也没有查询所有的权限";
		}elseif(empty($this->post['id'])){
			$json['warn'] = "收支平衡ID为空";
		}elseif(empty($profit['id'])){
			$json['warn'] = "未找到此收支记录";
		}else{
			$json['profit'] = array(
				"target" => $profit['target'],//来源
				"direction" => $profit['direction'],//方向
				"type" => $profit['type'],//类型
				"money" => $profit['money'],//金额
				"ico" => root.$profit['ico'],//付款凭证
				"text" => $profit['text'],//备注
				"payDate" => $profit['payDate'],//发生日期
			);
			$json['warn'] = "success";
		}
		//返回
		return $json;
	}
}
?>